package io.github.lee.jel.ui.list

import android.content.Context
import android.content.Intent
import androidx.appcompat.widget.Toolbar
import androidx.core.os.bundleOf
import androidx.databinding.ViewDataBinding
import dagger.hilt.android.AndroidEntryPoint
import io.github.lee.core.ui.BaseActivity
import io.github.lee.core.ui.databinding.ToolbarSimpleCenterTitleBinding
import io.github.lee.core.util.getBundleParcelable
import io.github.lee.core.vm.BaseViewModel
import io.github.lee.jel.R
import io.github.lee.jel.global.theme.ThemeManager
import io.github.lee.repository.jel.data.session
import io.github.lee.repository.jel.domain.Item

@AndroidEntryPoint
class ItemListActivity : BaseActivity<ViewDataBinding, BaseViewModel>() {

    companion object {
        private const val KEY_PARENT_ITEM = "ITEM"

        @JvmStatic
        fun start(context: Context, parentItem: Item) {
            val starter = Intent(
                context, ItemListActivity::class.java
            ).putExtras(bundleOf(KEY_PARENT_ITEM to parentItem))
            context.startActivity(starter)
        }
    }


    //===Desc:=============================================================
    override fun onCreateToolbar(): Toolbar =
        ToolbarSimpleCenterTitleBinding.inflate(layoutInflater).also {
            it.bg = getColor(R.color.main_page_background)
            val parent = getBundleParcelable(
                KEY_PARENT_ITEM, Item::class.java
            )
            it.title = parent?.name ?: mContext.session?.name ?: ""
        }.toolbar

    override fun onSetViewData() {
        super.onSetViewData()
        statusBarColor(ThemeManager.statusBarColor(mContext), ThemeManager.isStatusBarDark())
        setNavigationIcon(R.drawable.ic_arrow_back, getColor(R.color.main_theme)) { finish() }
        val parent = getBundleParcelable(KEY_PARENT_ITEM, Item::class.java)
        if (null == parent) {
            toast(getString(R.string.error_param_missing))
            return
        }
        replaceFragment(ItemListFragment.newInstance(parent))
    }


}